//
//  YWCalenderViewController.m
//  YWCalendar
//
//  Created by YR on 2017/5/25.
//  Copyright © 2017年 YR. All rights reserved.
//

#import "YWCalendarViewController.h"

#import "BTInfiniteScrollView.h"
#import "YWSingleMonthView.h"
#import "NSCalendar+YWDateConvertor.h"
#import "YWWeekTopView.h"

@interface YWCalendarViewController () <BTInfiniteScrollViewDelegate, YWSingleMonthViewDelegate>

@property (nonatomic) BTInfiniteScrollView *scollerView;
@property (nonatomic) YWSingleMonthView *visibleMonthView;
@property (nonatomic) YWWeekTopView *topView;
@property (nonatomic) YWButton *didSelectButn;
@property (nonatomic) UILabel *timeLabel;

@end

@implementation YWCalendarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor grayColor];
    [self.view addSubview:self.scollerView];
    self.scollerView.frame = CGRectMake(0, 100, self.view.bounds.size.width, self.view.bounds.size.height - 100);
    [self.view addSubview:self.topView];
    self.topView.frame = CGRectMake(0, 70, self.view.bounds.size.width, 30);
    [self.view addSubview:self.timeLabel];
    self.timeLabel.frame = CGRectMake(0, 30, self.view.bounds.size.width, 30);
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

#pragma mark - BTInfiniteScrollViewDelegate method
- (UIView *)infiniteScrollView:(BTInfiniteScrollView *)infiniteScrollView
                  viewForIndex:(NSInteger)index
                     thickness:(CGFloat *)thickness {
    if (index == NSNotFound) {
        return nil;
    }
    // index 为 0 表示当前月
    NSDateComponents *today = 
    [[NSCalendar currentCalendar] yw_yearMonthDayFromDate:[NSDate date]];
    NSInteger year = (today.year * 12 + today.month - 1 + index) / 12;
    NSInteger month = (today.year * 12 + today.month - 1 + index) % 12 + 1;
    YWSingleMonthView *view = [[YWSingleMonthView alloc] initWithYear:year andMonth:month];
    view.delegate = self;
    if (view.didSelectButn) {
        _didSelectButn = view.didSelectButn;
        [self changeTitleLabelText];
    }
    return view;
}

- (void)infiniteScrollView:(BTInfiniteScrollView *)infiniteScrollView
               didShowView:(UIView *)view
                   atIndex:(NSInteger)index {
    assert([view isKindOfClass:[YWSingleMonthView class]]);
    YWSingleMonthView *singleMonthView = (YWSingleMonthView*)view;
    self.visibleMonthView = singleMonthView;
    [self showTheFirstDayOfTheMonth:singleMonthView];
}

- (void)showTheFirstDayOfTheMonth:(YWSingleMonthView *)singleMonthView {
    
    NSDateComponents *dateCompnents = [[NSDateComponents alloc] init];
    dateCompnents.year = singleMonthView.model.currentYear;
    dateCompnents.month = singleMonthView.model.currentMonth;
    dateCompnents.day = 1;
}

- (void)didSelectButton:(YWButton *)sender {
    if (![sender isEqual:_didSelectButn]) {
        _didSelectButn.didSelected = NO;
        sender.didSelected = YES;
        _didSelectButn = sender;
        [self changeTitleLabelText];
    }
}

- (void)changeTitleLabelText {
    NSDateComponents *day = [[NSCalendar currentCalendar] yw_yearMonthDayFromDate:_didSelectButn.dayDate];
    _timeLabel.text = [NSString stringWithFormat:@"%@年 %@月 %@日",[@([day year]) stringValue],[@([day month]) stringValue],[@([day day]) stringValue]];
}

- (BTInfiniteScrollView *)scollerView {
    if (!_scollerView) {
        _scollerView = [[BTInfiniteScrollView alloc] initWithFrame:CGRectZero delegate:self horizontal:YES position:BTPositionStart];
        _scollerView.pagingEnabled = YES;
    }
    return _scollerView;
}

- (YWWeekTopView *)topView {
    if (!_topView) {
        _topView = [[YWWeekTopView alloc] init];
        _topView.frame = CGRectMake(0, 20, self.view.frame.size.width, 30);
    }
    return _topView;
}

- (UILabel *)timeLabel {
    if (!_timeLabel) {
        _timeLabel = [[UILabel alloc] init];
        _timeLabel.textAlignment = NSTextAlignmentCenter;
        _timeLabel.textColor = [UIColor whiteColor];
        _timeLabel.font = [UIFont systemFontOfSize:16];
    }
    return _timeLabel;
}

@end
